<template>
  <div class="setting-container">
    <el-form ref="form" :model="formData" :rules="rules" class="setting-form">
      <el-form-item label="机构名称" prop="organName">
        <el-input
          v-model.trim="formData.organName"
          placeholder="请输入机构名称"
          tabindex="1"
          type="text"
        />
      </el-form-item>
      <el-form-item label="地址" prop="address">
        <el-input
          v-model.trim="formData.address"
          placeholder="请输入地址"
          type="text"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="phone">
        <el-input
          v-model.trim="formData.phone"
          placeholder="请输入手机号"
          type="text"
        />
      </el-form-item>
      <el-form-item label="校区" prop="campus">
        <el-input
          v-model.trim="formData.campus"
          placeholder="请输入校区"
          type="text"
        />
      </el-form-item>
      <el-form-item label="联系人" prop="contacts">
        <el-input
          v-model.trim="formData.contacts"
          placeholder="请输入联系人"
          type="text"
        />
      </el-form-item>

      <el-button
        :loading="loading"
        class="login-btn"
        type="primary"
        @click="handleSubmit"
      >
        提交
      </el-button>
    </el-form>
  </div>
</template>

<script>
  import { mapGetters } from 'vuex'
  import { updateOrgan } from '@/apis/organ.js'
  import { validateUsername } from '@/utils/validator'

  export default {
    name: 'Setting',
    data() {
      return {
        formData: {
          organName: '',
          phone: '',
          province: '',
          city: '',
          region: '',
          address: '',
          campus: '',
          contacts: '',
        },
        rules: {
          organName: [
            {
              required: true,
              trigger: 'change',
              validator: validateUsername,
            },
          ],
        },
        loading: false,
      }
    },
    computed: {
      ...mapGetters({
        organInfo: 'user/organInfo',
      }),
    },
    created() {
      this.formData = {
        ...this.organInfo,
      }
    },
    methods: {
      handleSubmit() {
        this.$refs.form.validate(async (valid) => {
          if (valid) {
            try {
              this.loading = true
              await updateOrgan(this.formData)
              this.$baseMessage('操作成功', 'success')
            } finally {
              this.loading = false
            }
          }
        })
      },
    },
  }
</script>

<style lang="scss" scoped>
  .setting-form {
    width: 500px;
  }
</style>
